package angel.prototype.web.filter;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import com.google.javascript.jscomp.CompilationLevel;
//import com.google.javascript.jscomp.Compiler;
//import com.google.javascript.jscomp.CompilerOptions;
//import com.google.javascript.jscomp.SourceFile;
////import com.google.javascript.jscomp.JSSourceFile;
//import com.google.javascript.jscomp.WarningLevel;

public class ClosureCompilerFilter implements Filter {

	private final Map<String, String> compressedJs = new HashMap<String, String>();
    private FilterConfig filterConfig;

	public void init(FilterConfig filterConfig) throws ServletException {
		 this.filterConfig = filterConfig;	        
	     //Turn off the compiler log
//	     Compiler.setLoggingLevel(Level.OFF);
	}
	
	public void destroy() {
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
		 HttpServletRequest req = (HttpServletRequest) request;
	     HttpServletResponse resp = (HttpServletResponse) response;
	        
	        String uri = req.getRequestURI();
	        System.out.println("[ClosureCompilerFilter]uri:" +uri);
//	        //Prevent compress the compressed Javascript, such as jquery.min.js, mootools.min.js, etc.
//	        if (!uri.matches(".*\\.min\\.js$")){
//	            String js = null;
//	            
//	            if (compressedJs.containsKey(uri)){
//	                //get from cache
//	                js = compressedJs.get(uri);
//	            }else{
//	                String contextPath = req.getContextPath();
//	                String jsPath = uri.substring(contextPath.length());
//	                
//	                //get javascript file as stream
//	                //getResourceAsStream cannot include context path
//	                InputStream inputStream = filterConfig.getServletContext().getResourceAsStream(jsPath);
//	                js = compress(inputStream);
//	                
//	                //put to cache
//	                compressedJs.put(uri, js);
//	            }
//	            
//	            resp.getWriter().write(js);
//	            return;
//	        }
	        
	        chain.doFilter(request, response);
	        
	}
//
//	protected String compress(InputStream inputStream) throws IOException{
//        Compiler compiler = new Compiler();
//        
//        CompilerOptions options = new CompilerOptions();
//        CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
//        WarningLevel.QUIET.setOptionsForWarningLevel(options);
//        
//        SourceFile extern = SourceFile.fromCode("externs.js", "");
//        SourceFile input = SourceFile.fromInputStream("origin.js", inputStream);
//        compiler.compile(extern, input, options);
//       // compiler.compile(extern, input, options)
//        return compiler.toSource();
//    }
}
